package com.filyar.htyd.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.filyar.htyd.common.Result;
import com.filyar.htyd.entity.CompareTask;
import com.filyar.htyd.service.ICompareTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author filyar
 * @since 2024-10-25
 * @e-mail root@filyar.com
 */
@RestController
@RequestMapping("/compareTask")
public class CompareTaskController {

    @Resource
    private ICompareTaskService compareTaskService;

    private final String now = DateUtil.now();

    @PostMapping
    public Result save(@RequestBody CompareTask compareTask) {
        compareTaskService.saveOrUpdate(compareTask);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        compareTaskService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        compareTaskService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(compareTaskService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<CompareTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(compareTaskService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }



}

